package com.pxxy.mall.product.service.impl;

import com.pxxy.mall.product.dao.BrandDao;
import com.pxxy.mall.product.dao.CategoryDao;
import com.pxxy.mall.product.entity.BrandEntity;
import com.pxxy.mall.product.entity.CategoryEntity;
import com.pxxy.mall.product.vo.CategoryBrandRelationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pxxy.common.utils.PageUtils;
import com.pxxy.common.utils.Query;

import com.pxxy.mall.product.dao.CategoryBrandRelationDao;
import com.pxxy.mall.product.entity.CategoryBrandRelationEntity;
import com.pxxy.mall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private BrandDao brandDao;

    @Autowired
    private CategoryDao categoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public List<CategoryBrandRelationVo> getBrandsByCatId(Long catId) {
        List<CategoryBrandRelationEntity> entities = this.lambdaQuery().eq(CategoryBrandRelationEntity::getCatelogId, catId).list();
        return entities.stream().map(t -> {
            CategoryBrandRelationVo brandRelationVo = new CategoryBrandRelationVo();
            BeanUtils.copyProperties(t, brandRelationVo);
            return brandRelationVo;
        }).collect(Collectors.toList());

    }


    @Override
    public boolean save(CategoryBrandRelationEntity entity) {
        Long brandId = entity.getBrandId();
        Long catelogId = entity.getCatelogId();
        //查询品牌名，和分类名
        BrandEntity brandEntity = brandDao.selectById(brandId);
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        entity.setBrandName(brandEntity.getName());
        entity.setCatelogName(categoryEntity.getName());
        return super.save(entity);
    }
}